﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using DFGK.IRepository;
using DFGK.IRepository.BBS;
using DFGK.IService.BBS;
using DFGK_WMS.Model.BBS;
using DFGK_WMS.Model.Models.BBS;
using NPOI.HPSF;

namespace DFGK.Service.BBS
{
    public class InfomationTypeService : BaseService, IInfomationTypeService
    {
        private readonly IInfomationTypeRepository _infoTypeRepo;
        private readonly IUnitOfWork _unitOfWork;
        public InfomationTypeService(IMapper mapper, IInfomationTypeRepository infoTypeRepo, IUnitOfWork unitOfWork) : base(mapper)
        {
            _infoTypeRepo = infoTypeRepo;
            _unitOfWork = unitOfWork;
        }

        public List<InfomationType> GetInfoTypePageData(string typeName, out int totalCount, int pageIndex, int pageSize)
        {
            var query = _infoTypeRepo.GetEntityAll();
            if (!string.IsNullOrEmpty(typeName))
            {
                query = query.Where(t => t.TypeName == typeName);
            }
            totalCount = query.Count();
            query = query.Skip((pageIndex - 1) * pageSize).Take(pageSize).OrderByDescending(o => o.Id);
            return query.ToList();
        }


        public bool SubmitInfoTypeData(InfomationType model, out string errorMsg)
        {
            errorMsg = "";
            if (model.Id > 0)
            {
                _infoTypeRepo.UpdEntity(model);
            }
            else
            {
                _infoTypeRepo.AddEntity(model);
            }
            return _unitOfWork.SaveChangesEntity();
        }

        public InfomationType? GetInfoType(long infoTypeId)
        {
            return _infoTypeRepo.QueryEntityById(infoTypeId);
        }

        public bool DelInfoType(long shareInfoId)
        {
            var shareFileInfo = _infoTypeRepo.QueryEntityById(shareInfoId);
            if (shareFileInfo == null)
                return false;
            _infoTypeRepo.DelEntity(shareFileInfo);
            return _unitOfWork.SaveChangesEntity();
        }
    }
}
